package com.boot.security.server.service.impl.mffl.MfflLawfirm;

import com.boot.security.server.dao.mffl.MfflLawfirmDao;
import com.boot.security.server.dao.mffl.MfflTypeDao;
import com.boot.security.server.model.mffl.MfflLawfirm;
import com.boot.security.server.model.mffl.MfflType;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 *律所service
 *@author  诺高
 *@date    2019/12/13
 */
@Service
public class MfflLawfirmService {
    @Autowired
    private MfflLawfirmDao mfflLawfirmDao;

    @Autowired
    private MfflTypeDao mfflTypeDao;

    /**保存业务(后台)*/
    public MfflLawfirm save(@RequestBody MfflLawfirm mfflLawfirm) {
        mfflLawfirm.setDeleteState(1);
        mfflLawfirm.setDeleteTime(new Date());
        mfflLawfirmDao.save(mfflLawfirm);
        return mfflLawfirm;
    }


    /**数据列表（接口）*/
    public ReturnBean<MfflLawfirm> list(PageTableRequest request) {
        request.getParams().put("deleteState", 1);
        PageTableResponse handle = new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflLawfirmDao.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<MfflLawfirm> list(PageTableRequest request) {
                return mfflLawfirmDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        //通过id转为name
        List<MfflLawfirm> data = (List<MfflLawfirm>) handle.getData();
        for (MfflLawfirm item : data) {
            StringBuilder field = new StringBuilder();
            String[] split = item.getField().split(",");
            for (int i = 0; i < split.length; i++) {
                MfflType byId = mfflTypeDao.getById(Long.parseLong(split[i]));
                if (i + 1 == split.length) {
                    field.append(byId.getName());
                } else {
                    field.append(byId.getName() + ",");
                }
            }
            item.setField(field.toString());
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(handle);
        return returnBean;
    }


    /**获取详细信息（接口）*/
    public ReturnBean<MfflLawfirm> get(@PathVariable Long id) {
        ReturnBean returnBean = new ReturnBean();
        MfflLawfirm byId = mfflLawfirmDao.getById(id);
        StringBuilder field = new StringBuilder();
        String[] split = byId.getField().split(",");
        for (int i = 0; i < split.length; i++) {
            MfflType typebyId = mfflTypeDao.getById(Long.parseLong(split[i]));
            if(i+1 == split.length){
                field.append(typebyId.getName());
            }else{
                field.append(typebyId.getName() + ",");
            }
        }
        byId.setField(field.toString());
        returnBean.setDatas(byId);
        return returnBean;
    }
}
